const http = require("http");
const socket = require("socket.io");

let httpServer= http.createServer((req,res)=>{
  
}).listen(8081);
let wsServer = socket.listen(httpServer);
let socks = [];
let users = [];
wsServer.on("connection",sock=>{;
  console.log(sock.id)
  socks.push(sock)
  sock.on("msg",str=>{
    socks.forEach(sk=>{
      if(sock!=sk){
        sk.emit("msg",str)
      }
    })
  })
  sock.on("disconnect",()=>{
    console.log(sock.userName);
    let index = socks.indexOf(sock);
    let userindex = users.indexOf(sock.userName);
    if(index!=-1){
      socks.splice(index,1);
    }
    if(userindex!=-1){
      users.splice(userindex,1);
    }
  })
  
})




